package com.eastcom.mystope.daemon.provider;


import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.jdbc.support.rowset.SqlRowSetMetaData;
import org.springframework.stereotype.Component;


import com.eastcom.mystope.common.dao.hibernate4.CommonHibernateDao;
import com.eastcom.mystope.daemon.config.DbInfo;
import com.eastcom.mystope.daemon.config.Table;
import com.eastcom.mystope.daemon.context.ServerContext;
import com.eastcom.mystope.daemon.tools.JdbcUtil;

@Component("dbEngine")
@Scope("prototype")
public class DbEngine{
	@Resource(name = "jdbcUtil")
	private JdbcUtil jdbcUtil;
	private static Log logger = LogFactory.getLog(DbEngine.class);
	@Resource(name = "CommonHibernateDao")
	private CommonHibernateDao hibernateDao;
	public DbEngine() {
		super();
		// TODO 自动生成的构造函数存根
	}
	//取得当前数据库的表信息
	public boolean getTableInfo(String dbId, String tableName) {
		if(ServerContext.dbMap.containsKey(dbId)){
			DbInfo dbInfo = ServerContext.dbMap.get(dbId);
			if(dbInfo.getTables().containsKey(tableName)){
				return true;
			}
		}
		System.out.println(ServerContext.getCustomerType());
		// TODO 自动生成的方法存根
		String sql = "select * from "+tableName;
		logger.debug(sql);
		List<String> columnNames = new ArrayList<String>(10);
		List<Integer> columnTypes = new ArrayList<Integer>(10);
		List<String> primaryKeys = new ArrayList<String>(10);
		try {
			ResultSet result = jdbcUtil.queryPageAbsolute(sql,null,null, 1, 1);
			ResultSetMetaData metaData = result.getMetaData();
			int count = metaData.getColumnCount();
			for(int i = 1; i <= count; i++){
				columnNames.add(metaData.getColumnName(i));
				System.out.println(metaData.getColumnType(i));
				//将date转成timestamp
				if(metaData.getColumnType(i) == Types.DATE){
					columnTypes.add(Types.TIMESTAMP);
				}else{
					columnTypes.add(metaData.getColumnType(i));
				}
				System.out.println(metaData.getColumnName(i)+metaData.getColumnClassName(i));
			}
			ResultSet rst = jdbcUtil.getDataSource().getConnection().getMetaData().getPrimaryKeys(null, null, tableName);
			while(rst.next()){
				primaryKeys.add(rst.getString("COLUMN_NAME"));
				System.out.println(rst.getString("TABLE_NAME") + "  " +
						rst.getString("COLUMN_NAME"));
			}
			//数据库信息
			//考虑数据库名key是否重复
			if(! ServerContext.dbMap.containsKey(dbId)){
				DbInfo dbInfo = new DbInfo();
				dbInfo.setName(dbId);
				Map<String, Table> tables = new HashMap<String, Table>();

				Table table = new Table();
				table.setFeildName(columnNames);
				table.setPrimaryKeys(primaryKeys);
				table.setName(tableName);
				table.setFeildType(columnTypes);
				tables.put(tableName, table);
				dbInfo.setTables(tables);
				ServerContext.dbMap.put(dbId, dbInfo);
			}else{
				Table table = new Table();
				table.setFeildName(columnNames);
				table.setPrimaryKeys(primaryKeys);
				table.setName(tableName);
				table.setFeildType(columnTypes);
				ServerContext.dbMap.get(dbId).getTables().put(tableName, table);
			}

		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public void batchUpdateSQL(String sql, List<Object[]> param, int[]types) {
		// TODO 自动生成的方法存根
		jdbcUtil.batchUpdate(sql, param, types);
	}

	public boolean executeQuerySQL(String sql) {
		// TODO 自动生成的方法存根
		SqlRowSet rowSet = jdbcUtil.queryForRowSet(sql);
		SqlRowSetMetaData setMetaData = rowSet.getMetaData();
		String[] tableNames = setMetaData.getColumnNames();
		int columnNumber = setMetaData.getColumnCount();
		for(String string: tableNames){
			System.out.println("info"+string);
		}
		return false;
	}

	public boolean executeUpdateSQL(String sql, Object[] param, int[]types) {
		// TODO 自动生成的方法存根
		int cnt = jdbcUtil.update(sql, param, types);
		System.out.println(cnt);
		return cnt > 0 ? true : false;
	}

	public boolean executeStoredProcedure(String sql, Object[] param, int[]types) {
		// TODO 自动生成的方法存根
		return false;
	}

	public JdbcUtil getJdbcUtil() {
		return jdbcUtil;
	}
	public void setJdbcUtil(JdbcUtil jdbcUtil) {
		this.jdbcUtil = jdbcUtil;
	}
	public CommonHibernateDao getHibernateDao() {
		return hibernateDao;
	}
	public void setHibernateDao(CommonHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}


}
